#include "CocosPCH.h"
#include "ModelViewer.h"

ModelViewer::ModelViewer()
{
    m_bHasExportOne = false;
    m_bHasExportAll = false;
}

ui::Widget* ModelViewer::createItem(int nIndex, std::string strName)
{
    auto button = ui::Button::create("Button_Normal.png");
    button->setContentSize(Size(104, 40));
    button->setScale9Enabled(true);
    button->setTitleText(strName);
    button->setTitleFontSize(15);
    button->setTitleColor(Color3B(0, 0, 0));

    return button;
}

bool ModelViewer::LoadFile(std::string strPath)
{
    Sprite3D *sprite = Sprite3D::create(strPath);
    if (!sprite)
        return false;

    BaseViewer::LoadFile(strPath);

    sprite->setCullFaceEnabled(false);
    auto animation = Animation3D::create(strPath);
    if (animation)
    {
        auto animate = Animate3D::create(animation);
        sprite->runAction(RepeatForever::create(animate));
    }

    AABB aabb = sprite->getAABB();
    Vec3 corners[8];
    aabb.getCorners(corners);
    if (abs(corners[3].x) == 99999.0f && abs(corners[3].y) == 99999.0f && abs(corners[3].z) == 99999.0f)
    {
        _orginCenter = Vec3(0.0f, 0.0f, 0.0f);
        _orginDistance = 100.0f;
    }
    else
    {
        float radius = (corners[0] - corners[5]).length();
        _orginCenter = aabb.getCenter();
        _orginDistance = radius;
    }
    sprite->setCameraMask((unsigned short)CameraFlag::USER1);
    m_pViewNode->addChild(sprite);

    setCamera();
    resetCamera();

    return true;
}


void ModelViewer::ClearCache()
{
    Sprite3DCache::getInstance()->removeAllSprite3DData();
}